package com.mudingkou.mos.common.exception;


import com.mudingkou.mos.common.Message;
import com.mudingkou.mos.common.enums.CommonResultEnum;

import java.text.MessageFormat;

/**
 * <p>基础异常类，所有自定义异常类都需要继承本类</p>
 *
 * @author kzc
 * @date 2020/5/2
 */
public class BaseException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public BaseException() {
        this(CommonResultEnum.ERROR_UNKNOWN);
    }

    public BaseException(Message message) {
        super(message.getMessage());
        this.message = message;
    }

    public BaseException(int code, String message) {
        super(message);
        this.message = new Message() {
            @Override
            public int getCode() {
                return code;
            }

            @Override
            public String getMessage() {
                return message;
            }
        };
    }

    public BaseException(Message message, Object[] args) {
        super(MessageFormat.format(message.getMessage(), args));
        this.message = message;
    }

    public BaseException(Message message, Object[] args, Throwable cause) {
        super(MessageFormat.format(message.getMessage(), args), cause);
        this.message = message;
    }


    public String getMessage() {
        return this.message.getMessage();
    }
    public int getCode() {
        return this.message.getCode();
    }
    /**
     * 返回码
     */
    protected Message message;



}
